package gern;

import gern.anno.MyColumn;
import gern.anno.MyPrimaryKey;
import gern.anno.MyTable;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;

public class AbstractMapper<T> implements Mapper<T> {

    private String pk;
    private Class clazz;
    private String tableName;
    private TwoWayMap<String, String> twoWayMapColumn = new TwoWayMap<String, String>();

    public AbstractMapper() {
        Type type = this.getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) type;
        Type types[] = pt.getActualTypeArguments();
        clazz = (Class) types[0];
        MyTable anno = (MyTable) clazz.getAnnotation(MyTable.class);
        tableName = anno.name();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(MyColumn.class)) {
                String columnName = field.getAnnotation(MyColumn.class).column();
                twoWayMapColumn.add(field.getName(), columnName);
            }
            if (field.isAnnotationPresent(MyPrimaryKey.class)) {
                String id = field.getAnnotation(MyPrimaryKey.class).column();
                twoWayMapColumn.add(field.getName(), id);
                pk = id;
            }
        }
    }

    public T get(int page, int pageSize, String field, String sort, String keyword) {
        return null;
    }

    public T getByPrimaryKey() {

        String sql = getColumnWithCommaSplit();
        return query(sql);
    }

    /**
     * 获取使用逗号隔开的所有列名字符串
     *
     * @return id, na_me, my_passwd
     */
    private String getColumnWithCommaSplit() {
        StringBuilder re = new StringBuilder();
        for (String column : twoWayMapColumn.getValues()) {
            re.append(column).append(",");
        }
        return re.subSequence(0, re.length() - 1).toString();

    }

    public T getByCustomerKey() {
        return null;
    }

    public void insert(T t) {

    }

    public void update(T t) {

    }

    public void updateSelective(T t) {

    }

    public void updateWithCheck(T t) {

    }

    public void deleteByPrimaryKey() {

    }

    public void truncate() {

    }


    private T query(Object... o) {

        T t = null;
        try {
            t = (T) clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(MyColumn.class)) {
                    String columnName = field.getAnnotation(MyColumn.class).column();
                    field.setAccessible(true);
                    if (DemoUtil.getFiledType(field.getName(), t).equals(Date.class)) {
                        field.set(t, new Date());
                    } else {
                        field.set(t, twoWayMapColumn.getByValue(columnName));
                    }
                }
                if (field.isAnnotationPresent(MyPrimaryKey.class)) {
                    field.setAccessible(true);
                    field.set(t, "thisisid");
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return t;

    }

}
